package chatGUI;

import java.util.Observable;
import java.util.Observer;


import javax.swing.JOptionPane;

import model.SharingFiles;
import model.SharingMessages;

import javax.swing.JOptionPane;


import model.StateChangeException;
import model.SystemState;
import model.User;
import model.Users;
import chatController.CharacterNotAcceptedException;
import chatController.ChatController;
import chatController.UsernameEmptyException;

/**
 * @author Michael & Pierre
 */
/**
 * Platform-independent chat GUI based on ToUser and FromUser interfaces
 */
public class ChatGUI implements Observer, ToUser, FromUser {

	/**
	 * Static instance of the chat controller to create relationships with the GUI
	 */
	private static ChatController controller;
	
	/**
	 * Static instance of the chat user list to create relationships with the GUI
	 */
	private static Users users;
	
	/**
	 * Static instance of the chat system's state to create relationships with the GUI
	 */
	private static SystemState state;
	
	/**
	 * Instance of the platform-specific chat GUI to forward received messages
	 */
	private JavaGUI instanceGUI;
	
	/**
	 * Username of the local user
	 */
	private String username = "";
	
	/**
	 * Current user the user is having a conversation with
	 */
	private User currentConvUser = null;
	
	
	/**
	 * Constructor creating relationships with the other chat components
	 * @param controller
	 * @param users
	 * @param systemState
	 * @param sharingMessages
	 * @param sharingFiles
	 */
	public ChatGUI(ChatController controller, Users users, SystemState systemState, SharingMessages sharingMessages, SharingFiles sharingFiles) {
		//this.controller = new ChatController(); // Pas sur que si on enl�ve cette ligne, �a marche.
		// Vaudrait mieux que si, sinon je te pete la gueule.
		ChatGUI.controller = controller;
		ChatGUI.users = users;
		ChatGUI.state = systemState;
		this.instanceGUI = new JavaGUI(controller, users, state, sharingMessages, sharingFiles);
	}
	
	
	/**
	 * Sets the value of the local user's username.
	 * @param u
	 */
	private void setUsername(String u) {
		this.username = u;
		this.instanceGUI.setUsername(u);
	}
	

	/**
	 * Forwards the connection action to the controller
	 */
	@Override
	public void connect() {
		this.username();
		try {
			ChatGUI.controller.performConnect(this.username);
		} catch (UsernameEmptyException e) {
			e.printStackTrace();
		} catch (CharacterNotAcceptedException e) {
			e.printStackTrace();
		}
	}


	/**
	 * Gets the local username from the platform-specific GUI
	 */
	@Override
	public void username() {
		this.username = this.instanceGUI.getUsername();
	}


	/**
	 * Forwards the action of closing the window to the controller
	 */
	@Override
	public void closeWindow() {
		try {
			ChatGUI.controller.performDisconnect();
		} catch (StateChangeException e) {
			e.printStackTrace();
		}
	}


	/**
	 * Sets the proper value of the selected conversation user
	 * @param selected
	 */
	@Override
	public void selectedContact(User selected) {
		currentConvUser = selected;
	}


	/**
	 * Forwards the send message action to the controller
	 * @param msg
	 */
	@Override
	public void sendButton(String msg) {
		ChatGUI.controller.performSend(msg, currentConvUser);
	}


	/**
	 * Forwards the connected notification to the platform-specific GUI
	 */
	@Override
	public void infoConnected() {
		this.instanceGUI.infoConnected();
	}


	/**
	 * Forwards the notification of the connection being aborted to the platform-specific GUI
	 */
	@Override
	public void connectionAborted() {
		this.instanceGUI.connectionAborted();
	}


	/**
	 * Forwards the display action of the list to the platform-specific GUI
	 */
	@Override
	public void displayList() {
		this.instanceGUI.displayList();
	}


	/**
	 * Forwards the opening of the appropriate conversation window to the platform-specific GUI
	 * @param selectedUser
	 */
	@Override
	public void openConversationWindow(String selectedUser) {
		this.instanceGUI.openConversationWindow(selectedUser);
	}


	/**
	 * Forwards the message display to the platform-specific GUI
	 * @param user
	 * @param msg
	 */
	@Override
	public void displayMessage(User user, String msg) {
		this.instanceGUI.displayMessage(user, msg);
	}


	/**
	 * Forwards the received message display to the platform-specific GUI
	 * @param user
	 * @param msg
	 */
	@Override
	public void displayRcvd(User user, String msg) {
		this.instanceGUI.displayRcvd(user,msg);
	}

	/**
	 * Forwards the sharing file confirmation message to the platform-specific GUI 
	 * @param user
	 * @param name
	 */
	@Override
	public void displayDemand(User user, String name) {
		this.instanceGUI.displayRcvd(user, name);
	}
	
	/**
	 * Forwards the disconnection action to the system controller
	 */
	@Override
	public void disconnect() {
		try {
			ChatGUI.controller.performDisconnect();
		} catch (StateChangeException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Forwards the window closed notification to the platform-specific GUI
	 */
	@Override
	public void windowClosed() {
		this.instanceGUI.windowClosed();
	}

	/**
	 * Forwards the update action called from an observable Model to the platform-specific GUI
	 * @param arg0
	 * @param arg1
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		this.instanceGUI.update(arg0, arg1);
	}

}
